package com.cxl.price.action.price;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import net.sf.json.JSONArray;
import net.sf.json.JsonConfig;
import net.sf.json.util.PropertyFilter;

import org.apache.struts2.convention.annotation.ParentPackage;

import com.cxl.price.bean.ItemCat;
import com.cxl.price.service.ItemCatService;
import com.summer.core.action.BaseAction;
import com.summer.core.bean.Pager;

/**
 * @about: 淘宝类目 action
 * @author: wwwlike
 * @create: 2011-07-03
 */
 
 
@ParentPackage("price")			
public class ItemCatAction extends BaseAction<ItemCat, ItemCatService> {

	public void setItemCat(ItemCat itemCat) {
		setBean(itemCat);
	}

	public ItemCat getItemCat() {
		return getBean();
	}

	@Resource
	public void setService(ItemCatService service) {
		this.service = service;
	}


	
	public String importItemCat() throws Exception
	{
		service.addItemCats();
		return result(LIST);
	}
	
	
	
	public String serachName() throws UnsupportedEncodingException
	{
		//new String(bean.getName().getBytes("ISO-8859-1"),"UTF-8")
		Pager page=new Pager();
		page.setProperty("name");
		page.setKeyword(bean.getName());
		List<ItemCat> list=service.findByPager(page).getList();
		List<ItemCat> itemCatList=new ArrayList<ItemCat>();
		for(ItemCat cat:list)
		{
			if(cat.getIs_parent()==false)
			{
				
				try
				{
				cat.setName(cat.getItemCat().getName()+"<"+cat.getName()+">|"+cat.getId());
				itemCatList.add(cat);
				}catch(Exception e)
				{
					e.printStackTrace();
				}
			}
			
		}
		JsonConfig config = new JsonConfig();
        config.setJsonPropertyFilter(new PropertyFilter() {
            public boolean apply(Object arg0, String arg1, Object arg2) {
                if (arg1.equals("brands")) {
                    return true;
                }  else if (arg1.equals("itemCat")) {
                    return true;
                } else {
                    return false;
                }
            }
        });
        JSONArray jsonObject = JSONArray.fromObject(itemCatList,config);
        String content=jsonObject.toString();
		return ajaxJson(content);
		
	}
}
